#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QMessageBox>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pickNum = new PickNum();
    m_pickNum->setWindowModality(Qt::ApplicationModal);

    m_sudokusolve = new SudokuSolve();

    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            m_puzzle[i][j] = 0;
            m_answer[i][j] = 0;
        }
    }
    m_click_x = -1;
    m_click_y = -1;
    m_manual = false;

    m_puzzle[0][0] = 8;

    m_puzzle[1][2] = 3;
    m_puzzle[1][3] = 6;

    m_puzzle[2][1] = 7;
    m_puzzle[2][4] = 9;
    m_puzzle[2][6] = 2;

    m_puzzle[3][1] = 5;
    m_puzzle[3][5] = 7;

    m_puzzle[4][4] = 4;
    m_puzzle[4][5] = 5;
    m_puzzle[4][6] = 7;

    m_puzzle[5][3] = 1;
    m_puzzle[5][7] = 3;

    m_puzzle[6][2] = 1;
    m_puzzle[6][7] = 6;
    m_puzzle[6][8] = 8;

    m_puzzle[7][2] = 8;
    m_puzzle[7][3] = 5;
    m_puzzle[7][7] = 1;

    m_puzzle[8][1] = 9;
    m_puzzle[8][6] = 4;

    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            m_answer[i][j] = m_puzzle[i][j];
        }
    }

    connect(m_pickNum, SIGNAL(sig_clickNum(int)), this, SLOT(slot_clickNum(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    qint64 time1 = QDateTime::currentDateTime().toMSecsSinceEpoch();
    bool b = m_sudokusolve->solve(m_answer, m_puzzle);
    qint64 time2 = QDateTime::currentDateTime().toMSecsSinceEpoch();
    if(!b)
    {
        QMessageBox::information(this, "警告", "此数独无解");
        return;
    }
    ui->label->setText("耗时：" + QString::number(time2 - time1) + "ms");
    update();
}

void MainWindow::slot_clickNum(int num)
{
    //qDebug() << "slot" << num;
    if(m_click_x == -1 || m_click_y == -1)
    {
        return;
    }

    bool b = m_sudokusolve->isOk(m_puzzle, m_click_x, m_click_y, num);
    if(!b)
    {
        QMessageBox::information(this, "警告", "填写错误");
    }
    else
    {
        m_puzzle[m_click_x][m_click_y] = num;
        update();
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    for (int i = 0; i < 10; i++)
    {
        if(i % 3 == 0)
        {
            painter.setPen(QPen(Qt::black, 3));
        }
        else
        {
            painter.setPen(QPen(Qt::black, 1));
        }
        painter.drawLine(30, 30 + i * 50, 480, 30 + i * 50);
    }
    for (int i = 0; i < 10; i++)
    {
        if(i % 3 == 0)
        {
            painter.setPen(QPen(Qt::black, 3));
        }
        else
        {
            painter.setPen(QPen(Qt::gray, 1));
        }
        painter.drawLine(30 + i * 50, 30, 30 + i * 50, 480);
    }

    painter.setFont(QFont("黑体", 30));
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            if(m_puzzle[i][j] == 0 && m_answer[i][j] != 0)
            {
                painter.setPen(QPen(Qt::red, 3));
                painter.drawText(30 + 50 * j + 18, 30 + 50 * i + 38, QString::number(m_answer[i][j]));
            }
            else if(m_puzzle[i][j] != 0)
            {
                painter.setPen(QPen(Qt::black, 3));
                painter.drawText(30 + 50 * j + 18, 30 + 50 * i + 38, QString::number(m_puzzle[i][j]));
            }
        }
    }
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(!m_manual)
    {
        return;
    }
    m_click_x = (e->y() - 30) / 50;
    m_click_y = (e->x() - 30) / 50;
    m_pickNum->setNum(m_puzzle[m_click_x][m_click_y]);
    m_pickNum->show();
}

void MainWindow::on_pushButton_2_clicked()
{
    if(!m_manual)
    {
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                m_puzzle[i][j] = 0;
                m_answer[i][j] = 0;
            }
        }
        m_manual = true;
        ui->pushButton_2->setText("完成");
        ui->label->setText("耗时：0ms");
        ui->pushButton->setDisabled(true);
    }
    else
    {
        m_manual = false;
        ui->pushButton_2->setText("填写数字");
        ui->pushButton->setDisabled(false);
    }
    update();
}

void MainWindow::on_pushButton_3_clicked()
{
    qint64 time1 = QDateTime::currentDateTime().toMSecsSinceEpoch();
    bool b = m_sudokusolve->solve2(m_answer, m_puzzle);
    qint64 time2 = QDateTime::currentDateTime().toMSecsSinceEpoch();
    if(!b)
    {
        QMessageBox::information(this, "警告", "此数独无解");
        return;
    }
    ui->label->setText("耗时：" + QString::number(time2 - time1) + "ms");
    update();
}
